import { EnvEnum } from '@app/common/enum/envEnum';
import { ConfigService } from '@nestjs/config';
import { Controller, Get } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { Crud } from 'nestjs-mongoose-crud';
import { InjectModel } from 'nestjs-typegoose';
import { Model } from 'mongoose';
import { Course } from '@libs/db/models/course.model';
import { Category } from '@libs/db/models/category.model';

@Controller('courses')
@ApiTags('课程')
@Crud({
  model: Course,
})
export class CoursesController {
  constructor(
    @InjectModel(Course) private readonly model: Model<Course>,
    @InjectModel(Category) private readonly categoryModel: Model<Category>,
    private readonly configService: ConfigService,
  ) {}

  @Get('options')
  async getOptions() {
    const categories = (await this.categoryModel.find()).map((cate) => ({
      label: cate.name,
      value: cate._id,
    }));
    const uploadUrl =
      this.configService.get('env')[
        process.env.NODE_ENV === 'produciton'
          ? EnvEnum.dev_upload
          : EnvEnum.pro_upload
      ];
    return {
      title: '课程管理',
      columns: [
        { label: 'ID', prop: '_id' },
        { label: '课程名称', prop: 'name', search: true },
        { label: '课程描述', prop: 'description' },
        {
          label: '课程分类',
          prop: 'categories',
          select: true,
          selectData: categories,
          multiple: true,
          column: true,
          multipleMax: 3,
        },
        {
          label: '课程封面',
          prop: 'cover',
          upload: true,
          uploadUrl: uploadUrl,
        },
      ],
    };
  }
}
